<template>
  <div class="between">
    <el-form ref="form" :model="form" label-width="120px" :rules="rules" size="small" :disabled="disabled">
      <slot name="form-item-task-nodeCode" :model="form" field="nodeCode">
        <el-form-item label="节点编码">
          <el-input v-model="form.nodeCode" :disabled="disabled"></el-input>
        </el-form-item>
      </slot>
      <slot name="form-item-task-nodeName" :model="form" field="nodeName">
        <el-form-item label="节点名称">
          <el-input v-model="form.nodeName" :disabled="disabled"></el-input>
        </el-form-item>
      </slot>
      <slot name="form-item-task-collaborativeWay" :model="form" field="collaborativeWay">
        <el-form-item label="协作方式">
          <el-radio-group v-model="form.collaborativeWay" @change="collaborativeWayChange">
            <el-radio label="1">
              或签
              <el-tooltip class="item" effect="dark" content="只需一个人审批">
                <i class="el-icon-question" style="color: #000"></i>
              </el-tooltip>
            </el-radio>
            <el-radio label="2">
              票签
              <el-tooltip class="item" effect="dark" content="部分办理人审批，只支持选择用户">
                <i class="el-icon-question" style="color: #000"></i>
              </el-tooltip>
            </el-radio>
            <el-radio label="3">
              会签
              <el-tooltip class="item" effect="dark" content="所有办理都需要审批，只支持选择用户">
                <i class="el-icon-question" style="color: #000"></i>
              </el-tooltip>
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </slot>
      <slot name="form-item-task-nodeRatio" :model="form" field="nodeRatio" v-if="form.collaborativeWay === '2'">
        <el-form-item label="票签占比" prop="nodeRatio">
          <el-input v-model="form.nodeRatio" type="number" placeholder="请输入"></el-input>
          <div class="placeholder">票签比例范围：(0-100)的值</div>
        </el-form-item>
      </slot>
      <slot name="form-item-task-permissionFlag" :model="form" field="permissionFlag">
        <el-tooltip
          effect="dark"
          :content="userNameList"
          :disabled="!disabled"
        >
          <!-- <el-form-item label="权限标识"> -->
          <el-form-item label="处理人选择">
            <!-- 按钮：点击弹框选择用户id -->
            <el-button type="primary" @click="getUserId">选择处理人</el-button>
            <!-- <input type="text" v-model="form.permissionFlag"> -->
            <!-- <div>{{form.permissionFlag }}</div> -->
            <!-- <el-select v-model="form.permissionFlag" multiple collapse-tags :disabled="disabled" :clearable="!disabled" filterable v-if="form.collaborativeWay === '1'">
              <el-option-group
                v-for="groupOption in groupOptions"
                :key="groupOption.label"
                :label="groupOption.label"
                :disabled="disabled">
                <el-option
                  v-for="item in groupOption.options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-option-group>
            </el-select> -->
            <!-- <el-select
              v-else
              v-model="form.permissionFlag"
              multiple
              collapse-tags
              :disabled="disabled"
              :clearable="!disabled"
              @focus="initUser"
              popper-class="dialogSelect"
              :popper-append-to-body="false"
            >
              <el-option
                v-for="item in form.permissionFlag"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select> -->
          </el-form-item>
        </el-tooltip>
      </slot>
      <slot name="form-item-task-anyNodeSkip" :model="form" field="anyNodeSkip">
        <el-form-item label="驳回到指定节点">
          <el-button type="primary" @click="getanynodeskip">选择驳回节点</el-button>
          <!-- <el-radio-group v-model="form.anyNodeSkip">
            <el-radio label="N">否</el-radio>
            <el-radio label="Y">是</el-radio>
          </el-radio-group> -->
        </el-form-item>
      </slot>
      <!-- <slot name="form-item-task-listenerType" :model="form" field="listenerType">
        <el-form-item label="监听器类型">
          <el-select v-model="form.listenerType" multiple>
            <el-option label="任务创建" value="create"></el-option>
            <el-option label="任务开始办理" value="start"></el-option>
            <el-option label="分派监听器" value="assignment"></el-option>
            <el-option label="权限认证" value="permission"></el-option>
            <el-option label="任务完成" value="finish"></el-option>
          </el-select>
        </el-form-item>
      </slot> -->
      <!-- <slot name="form-item-task-listenerPath" :model="form" field="listenerPath">
        <el-form-item label="监听器路径" description="输入监听器的路径，以@@分隔,顺序与监听器类型一致">
          <el-input type="textarea" v-model="form.listenerPath" rows="8"></el-input>
          <el-tooltip class="item" effect="dark" content="输入监听器的路径，以@@分隔，顺序与监听器类型一致">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item>
      </slot> -->
      <!-- <slot name="form-item-task-formCustom" :model="form" field="formCustom">
        <el-form-item label="审批表单是否自定义">
          <el-select v-model="form.formCustom">
            <el-option label="使用流程表单" :value="''"></el-option> -->
            <!-- <el-option label="节点自定义表单" value="Y"></el-option> -->
            <!-- <el-option label="节点表单路径" value="N"></el-option>
          </el-select>
        </el-form-item>
      </slot> -->
      <!-- <slot name="form-item-task-formPath" :model="form" field="formPath" v-if="form.formCustom === 'N'">
        <el-form-item label="审批表单路径">
          <el-input v-model="form.formPath"></el-input>
        </el-form-item>
      </slot> -->
    </el-form>

    <!-- 权限标识：会签票签选择用户 -->
    <!-- <el-dialog title="用户选择" :visible.sync="userVisible" width="80%" append-to-body> -->
      <!-- <selectUser :selectUser.sync="form.permissionFlag" :userVisible.sync="userVisible" @handleUserSelect="handleUserSelect"></selectUser> -->
    <!-- </el-dialog> -->
  </div>
</template>

<script>
// import {listRole} from "@/api/system/role";
// import {listUser, idReverseDisplayName} from "@/api/system/user";
// import {listDept} from "@/api/system/dept";
// import selectUser from "@/views/components/selectUser";

export default {
  name: "Between",
  // components: {
  //   selectUser
  // },
  props: {
    useridstring:{
      type: Array,
    },
    anynodeskip:{
      type:String
    },
    nodeencoding:{
      type:String
    },
    value: {
      type: Object,
      default () {
        return {}
      }
    },
    disabled: { // 是否禁止
      type: Boolean,
      default: false
    },
  },
  data () {
    return {
      form: this.value,
      userNameList: "",
      groupOptions: [],
      rules: {
        nodeRatio: [
          { required: false, message: "请输入", trigger: "change" },
          { pattern: /^(?:[1-9]\d?|0\.\d{1,3}|[1-9]\d?\.\d{1,3})$/, message: "请输入(0, 100)的值，最多保留三位小数", trigger: ["change", "blur"] }
        ]
      },
      userVisible: false
    }
  },
  watch: {
    // 监视平台的用户id变化，变化了就同步到form.permissionFlag中
    useridstring(newVal) {
      // 当userIdString变化时，同步到form.permissionFlag中
      console.log('======》between中的userIdString变化了',newVal)
      console.log('this.form的数据',this.form)
      this.form.permissionFlag = newVal;
      // 问题排查：vue对象.语法添加的属性不是响应式
      console.log('======》打印this.form,和监听的permissionFlag',this.form,newVal)
      this.$set(this.form,'permissionFlag', newVal);
      // 同步form的变化
      console.log('======》主动触发change事件')
      this.$emit('change', this.form)
      console.log('监听到useridstring变化后，手动触发父级watch')
      this.$emit('callFatherPermissionFlag',newVal)

    },
    // 监视平台的用户id变化，变化了就同步到form.anyNodeSkip中
    anynodeskip(newVal) {
      // 当userIdString变化时，同步到form.anyNodeSkip中
      console.log('======》between中的anyNodeSkip变化了',newVal)
      console.log('this.form的数据',this.form)
      this.form.anynodeskip = newVal;
      // 问题排查：vue对象.语法添加的属性不是响应式
      console.log('======》打印this.form,和监听的anyNodeSkip',this.form,newVal)
      this.$set(this.form,'anynodeskip', newVal);
      // 同步form的变化
      console.log('======》主动触发change事件')
      this.$emit('change', this.form)
      console.log('监听到anyNodeSkip变化后，手动触发父级watch')
      this.$emit('callFatherAnyNodeSkip',newVal)

    },
    form: {
      handler (n) {
        console.log('between中的form变化了,触发了：',n)
        this.$emit('change', n)
      },
      deep: true
    }
  },
  
  created() {
    console.log('触发了between.vue的created方法，该方法作用是把this.form.permissionFlag装换成数组--------------')
    console.log('触发了between.vue的created方法，此时的this.form.permissionFlag为：', this.form.permissionFlag)
    if (this.form.permissionFlag) {
      this.form.permissionFlag = this.form.permissionFlag.split(",")
    }
    if (this.form.listenerType) {
      this.form.listenerType = this.form.listenerType.split(",")
    }
    if (!this.form.permissionFlag) {
    console.log('触发了between.vue的created方法，进入了if判断，在此把this.form.permissionFlag赋值为[]--------------')
      this.form.permissionFlag = []
    }
    this.getPermissionFlag();
    if (this.disabled) this.getIdReverseDisplayName();
    console.log('触发了between.vue的created方法，执行完毕后的this.form.permissionFlag为：', this.form.permissionFlag)
  },
  methods: {
    // 获取焦点弹框获取用户id
    getUserId() {
      // 触发平台事件
      // 置空
      // this.form.permissionFlag = '';
      // 解决点击新增节点选择处理人时无法自动同步的问题，目前猜测因为没有this.form.permissionFlag为undefined，所以引用类型无法自动同步，所以对undefined做处理，如果是undefined则赋值为[]，这样就是引用数据类型了
      console.log('依赖库getuseid中获取的this.form.permissionFlag',this.form.permissionFlag)
      if (!this.form.permissionFlag) {
        console.log('this.form.permissionFlag重新赋值为[]了')
        this.form.permissionFlag = []
      }
      console.log('处理人-between.vue的事件触发了,往上一层传参的数据为',this.form.permissionFlag)
      this.$emit('callFather',this.form.permissionFlag);
    },
    // 获取焦点弹框获取驳回节点
    getanynodeskip() {
      // 触发平台事件
      // 置空
      // this.form.permissionFlag = '';
      console.log('驳回节点-between.vue的事件触发了,往上一层传参的数据为getanynodeskip',this.form.anyNodeSkip)
      this.$emit('callFather1',[this.form.anyNodeSkip,this.form.nodeCode]);
      // console.log('此时的nodeencoding：',this.nodeencoding)
      // console.log('此时的nodeCode：',this.form.nodeCode)

      this.nodeencoding = this.form.nodeCode
    },
    // 根据id查用户名
    getIdReverseDisplayName() {
      if (this.form.collaborativeWay !== "1") {
        // idReverseDisplayName(this.form.permissionFlag.join(",")).then(res => {
        //   this.userNameList = res.data ? res.data.map(e => e.nickName).join(",") : "";
        // });
      }
    },
    /** 选择角色权限范围触发 */
    getPermissionFlag() {
      let groupOptionCreateBy = {
        label: '创建人',
        options: [{
          value: 'warmFlowInitiator',
          label: '流程发起人'
        }]
      }
      this.groupOptions.push(groupOptionCreateBy);
      // listRole().then(response => {
      //   let groupOption = {
      //     label: '角色',
      //     options: response.rows.map(item =>{
      //         return {
      //           value: 'role:' + item.roleId,
      //           label: item.roleName
      //         }
      //       }
      //     )
      //   }
      //   this.groupOptions.push(groupOption);
      //   listUser().then(response => {
      //     let groupOption = {
      //       label: '用户',
      //       options: response.rows.map(item =>{
      //           return {
      //             value: item.userId,
      //             label: item.nickName
      //           }
      //         }
      //       )
      //     }
      //     this.groupOptions.push(groupOption);
      //     listDept().then(response => {
      //       let groupOption = {
      //         label: '部门',
      //         options: response.data.map(item =>{
      //             return {
      //               value: 'dept:' + item.deptId,
      //               label: item.deptName
      //             }
      //           }
      //         )
      //       }
      //       this.groupOptions.push(groupOption);
      //       if (this.disabled && this.form.collaborativeWay === "1") {
      //         let userNameList = [];
      //         this.groupOptions.forEach(e => {
      //           e.options.forEach(o => {
      //             if (this.form.permissionFlag.includes(o.value)) userNameList.push(o.label);
      //           });
      //         });
      //         this.userNameList = userNameList.join(",");
      //       }
      //     });
      //   });
      // });
    },
    collaborativeWayChange(val) {
      this.form.permissionFlag = [];
      this.$set(this.form, "nodeRatio", val === "1" ? "0.000" : val === "3" ? "100.000" : "");
    },
    // 打开用户选择弹窗
    initUser() {
      this.userVisible = true;
    },
    // 获取选中用户数据
    handleUserSelect(checkedItemList) {
      this.form.permissionFlag = checkedItemList.map(e => {
        return e.userId;
      });
    }
  }
}
</script>

<style scoped>
.dialogSelect {
  display: none;
}
.placeholder {
  color: #828f9e;
  font-size: 12px;
  justify-content: space-around;
}
</style>
